package pres.wchen.bussiness.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pres.wchen.bussiness.cor.model.BaseModel;
import pres.wchen.bussiness.service.SysConstantService;
import pres.wchen.bussiness.vo.BusConfigVO;
import pres.wchen.bussiness.service.BusConfigService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import pres.wchen.bussiness.vo.SysConstantVO;
import pres.wchen.bussiness.web.BaseController;

/**
 * @作者： wchen
 * @描述： 商户配置管理
 * @创建时间： 2018/03/04 22:12:04
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
@RestController
@RequestMapping("/bus/config")
public class BusConfigController extends BaseController {
	@Autowired
	private BusConfigService busConfigService;
	@Autowired
	private SysConstantService sysConstantService;


	/**
     *  @描述: 商户配置新增
     *  @参数： [busConfigVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/3/04
     *  @修改人和其它信息:
     */
	@PostMapping(value = "/add")
	public Map insert(BusConfigVO busConfigVO) {
		Long id = busConfigService.insert(busConfigVO);
		return super.dealInsertResult(id);
	}


	/**
     *  @描述: 商户配置删除
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/3/04
     *  @修改人和其它信息:
     */
	@DeleteMapping(value = "/delete/{id}")
	public Map delete(@PathVariable("id") Long id) {
		Integer result = busConfigService.delete(id);
		return super.dealDeleteResult(result);
	}


	/**
     *  @描述: 商户配置修改
     *  @参数： [busConfigVO]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/3/04
     *  @修改人和其它信息:
     */
	@PutMapping(value = "/modify/")
	public Map modify(BusConfigVO busConfigVO) {
		Integer result = busConfigService.update(busConfigVO);
		return super.dealUpdateResult(result);
	}


	/**
     *  @描述: 商户配置详情
     *  @参数： [id]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/3/04
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/info/{id}")
	public ModelAndView info(@PathVariable("id") Long id) {
	    Map conditionMap = new HashMap();
        conditionMap.put("id", id);
		BusConfigVO busConfigVO = busConfigService.info(conditionMap);
		return super.dealInfoResult("/bus/business/config/maintain_modify", busConfigVO);
	}


	/**
     *  @描述: 商户配置列表
     *  @参数： [request]
     *  @返回值： java.util.Map
     *  @创建人: wchen
     *  @创建时间: 2018/3/04
     *  @修改人和其它信息:
     */
	@GetMapping(value = "/pageList")
	public ModelAndView pageList(HttpServletRequest request) {
		Object busId = request.getParameter("busId");
		Object busName = request.getParameter("busName");
		Object showType = request.getParameter("showType");
		Map conditionMap = new HashMap();
		conditionMap.put("groupName", "BUS_CONFIG");
		conditionMap.put("pageFlag", false);
		BaseModel baseModel = sysConstantService.list(conditionMap);
		List<SysConstantVO> baseList = baseModel.getList();

		List<Map> dataList = new ArrayList<>();
		for(SysConstantVO constantVO : baseList){
			Map dataMap = new HashMap();
			dataMap.put("consLabel", constantVO.getConsLabel());
			dataMap.put("consName", constantVO.getConsName());
			conditionMap.put("consName", constantVO.getConsName());
			conditionMap.put("busId", busId);
			List configList = busConfigService.list(conditionMap).getList();
			dataMap.put("configList", configList);
			dataList.add(dataMap);
		}


		baseModel = new BaseModel();
		baseModel.setList(dataList);
		Map<String, String> remarkMap = new HashMap<>();
		remarkMap.put("busId", busId.toString());
		remarkMap.put("busName", busName.toString());
		baseModel.setRemark(remarkMap);

		if(showType == null){
			return super.dealListResult("/bus/business/config/maintain", baseModel);
		}else {
			return super.dealListResult("/bus/business/config/info", baseModel);
		}

	}


}
